package my.framework.web;

import my.framework.anno.RequestMapping;

import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class ControllerMappingManager {
    private Map<String, ControllerMapping> controllerMappings = new HashMap<String, ControllerMapping>();

    /**
     * 构造方法
     * @param webPackage web路径
     */
    public ControllerMappingManager(String webPackage) {
        init(webPackage);
    }

    /**
     * init方法来加载Controller配置文件
     * @param webPackage web路径
     */
    private void init(String webPackage) {
        System.out.println("ControllerMappingManager 初始化。。。。");
        //this.getClass()：获取当前类的 Class 对象
        //.getResource("/")：获取类路径下的根目录资源
        //.getPath()：获取该资源的文件系统路径
        String rootPath = this.getClass().getResource("/").getPath();
        //拆分包名，拼接包路径
        String[] packageNames = webPackage.split("\\.");
        for (int i = 0; i < packageNames.length; i++){
            rootPath += packageNames[i] + "/";
        }
        //创建web目录对象
        File file = new File(rootPath);
        //获取目录下的所有文件
        File[] files = file.listFiles();
        for (File tempFile : files){
            //获得文件名，利用replaceAll替换.class后缀
            String className = tempFile.getName().replaceAll(".class", "");
            try {
                Class<?> clazz = Class.forName(webPackage + "." + className);
                //获取该类的所有方法
                Method[] declaredMethods = clazz.getDeclaredMethods();
                for (Method declaredMethod : declaredMethods){
                    //循环判断方式是否添加@RequestMapping注解
                    if (declaredMethod.isAnnotationPresent(RequestMapping.class)){
                       //得到该方法上添加的@RequestMapping注解
                       RequestMapping declaredAnnotation =
                               declaredMethod.getDeclaredAnnotation(RequestMapping.class);
                        //创建ControllerMapping对象，设置类名和方法名
                        //创建一个 ControllerMapping 对象，用于保存控制器映射信息。
                        ControllerMapping controllerMapping = new ControllerMapping();
                        //设置控制器对应的类名（包括完整包路径）
                        controllerMapping.setClassName(webPackage + "." + className);
                        //设置控制器对应的方法名。
                        controllerMapping.setMethodName(declaredMethod.getName());
                        //将注解中的 value() 值作为键，将 ControllerMapping 对象存入 controllerMappings 映射表中。
                        controllerMappings.put(declaredAnnotation.value(), controllerMapping);
                    }
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        System.out.println("map==============>" + controllerMappings);
    }

    /**
     * 根据映射信息查询对应的ControllerMapping实例
     * @param path
     * @return ControllerMapping
     */
    public ControllerMapping getControllerMapping(String path) {
        return this.controllerMappings.get(path);
    }

    /**
     * 判断是否是对应的映射信息
     * @param path
     * @return
     */
    public boolean containKey(String path) {
        return this.controllerMappings.containsKey(path);
    }

    /**
     * @return 返回所有Controller信息
     */
    public Map<String, ControllerMapping> getControllerMappings() {
        return controllerMappings;
    }

}
